From 7bfddeeb4e8c7eefde640069dea14da8b1672309 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 3 Apr 2018 00:36:30 +0300 Subject: [PATCH] Replace triple with struct --- src/cargo/ops/cargo_clean.rs | 6 +-- .../cargo_rustc/context/compilation_files.rs | 50 ++++++++++++------- src/cargo/ops/cargo_rustc/context/mod.rs | 11 ++-- src/cargo/ops/cargo_rustc/fingerprint.rs | 8 +-- src/cargo/ops/cargo_rustc/mod.rs | 42 ++++++++-------- src/cargo/ops/cargo_rustc/output_depinfo.rs | 4 +- 6 files changed, 67 insertions(+), 54 deletions(-) diff --git a/src/cargo/ops/cargo_clean.rs b/src/cargo/ops/cargo_clean.rs index 466105225..30009c7a6 100644 --- a/src/cargo/ops/cargo_clean.rs +++ b/src/cargo/ops/cargo_clean.rs @@ -112,9 +112,9 @@ pub fn clean(ws: &Workspace, opts: &CleanOptions) -> CargoResult<()> { continue; } - for &(ref src, ref link_dst, _) in cx.target_filenames(unit)?.iter() { - rm_rf(src, config)?; - if let Some(ref dst) = *link_dst { + for output in cx.outputs(unit)?.iter() { + rm_rf(&output.path, config)?; + if let Some(ref dst) = output.hardlink { rm_rf(dst, config)?; } } diff --git a/src/cargo/ops/cargo_rustc/context/compilation_files.rs b/src/cargo/ops/cargo_rustc/context/compilation_files.rs index 81272fe8e..d78364a3a 100644 --- a/src/cargo/ops/cargo_rustc/context/compilation_files.rs +++ b/src/cargo/ops/cargo_rustc/context/compilation_files.rs @@ -29,13 +29,19 @@ pub struct CompilationFiles<'a, 'cfg: 'a> { pub(super) target: Option, ws: &'a Workspace<'cfg>, metas: HashMap, Option>, - /// For each Unit, a list all files produced as a triple of - /// - /// - File name that will be produced by the build process (in `deps`) - /// - If it should be linked into `target`, and what it should be called (e.g. without - /// metadata). - /// - Type of the file (library / debug symbol / else) - outputs: HashMap, LazyCell, FileFlavor)>>>>, + /// For each Unit, a list all files produced. + outputs: HashMap, LazyCell>>>, +} + +#[derive(Debug)] +pub struct OutputFile { + /// File name that will be produced by the build process (in `deps`). + pub path: PathBuf, + /// If it should be linked into `target`, and what it should be called + /// (e.g. without metadata). + pub hardlink: Option, + /// Type of the file (library / debug symbol / else). + pub flavor: FileFlavor, } impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> { @@ -153,13 +159,13 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> { } } - pub(super) fn target_filenames( + pub(super) fn outputs( &self, unit: &Unit<'a>, cx: &Context<'a, 'cfg>, - ) -> CargoResult, FileFlavor)>>> { + ) -> CargoResult>> { self.outputs[unit] - .try_borrow_with(|| self.calc_target_filenames(unit, cx)) + .try_borrow_with(|| self.calc_outputs(unit, cx)) .map(Arc::clone) } @@ -211,11 +217,11 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> { } } - fn calc_target_filenames( + fn calc_outputs( &self, unit: &Unit<'a>, cx: &Context<'a, 'cfg>, - ) -> CargoResult, FileFlavor)>>> { + ) -> CargoResult>> { let out_dir = self.out_dir(unit); let file_stem = self.file_stem(unit); let link_stem = self.link_stem(unit); @@ -229,11 +235,15 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> { let mut unsupported = Vec::new(); { if unit.profile.check { - let filename = out_dir.join(format!("lib{}.rmeta", file_stem)); - let link_dst = link_stem + let path = out_dir.join(format!("lib{}.rmeta", file_stem)); + let hardlink = link_stem .clone() .map(|(ld, ls)| ld.join(format!("lib{}.rmeta", ls))); - ret.push((filename, link_dst, FileFlavor::Linkable)); + ret.push(OutputFile { + path, + hardlink, + flavor: FileFlavor::Linkable, + }); } else { let mut add = |crate_type: &str, flavor: FileFlavor| -> CargoResult<()> { let crate_type = if crate_type == "lib" { @@ -250,11 +260,15 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> { match file_types { Some(types) => for file_type in types { - let filename = out_dir.join(file_type.filename(&file_stem)); - let link_dst = link_stem + let path = out_dir.join(file_type.filename(&file_stem)); + let hardlink = link_stem .as_ref() .map(|&(ref ld, ref ls)| ld.join(file_type.filename(ls))); - ret.push((filename, link_dst, file_type.flavor)); + ret.push(OutputFile { + path, + hardlink, + flavor: file_type.flavor, + }); }, // not supported, don't worry about it None => { diff --git a/src/cargo/ops/cargo_rustc/context/mod.rs b/src/cargo/ops/cargo_rustc/context/mod.rs index 5993c9225..f7456fa31 100644 --- a/src/cargo/ops/cargo_rustc/context/mod.rs +++ b/src/cargo/ops/cargo_rustc/context/mod.rs @@ -2,7 +2,7 @@ use std::collections::{HashMap, HashSet}; use std::env; -use std::path::{Path, PathBuf}; +use std::path::Path; use std::str::{self, FromStr}; use std::sync::Arc; @@ -24,7 +24,7 @@ mod unit_dependencies; use self::unit_dependencies::build_unit_dependencies; mod compilation_files; -use self::compilation_files::CompilationFiles; +use self::compilation_files::{CompilationFiles, OutputFile}; pub use self::compilation_files::Metadata; mod target_info; @@ -274,11 +274,8 @@ impl<'a, 'cfg> Context<'a, 'cfg> { /// - filename: filename rustc compiles to. (Often has metadata suffix). /// - link_dst: Optional file to link/copy the result to (without metadata suffix) /// - linkable: Whether possible to link against file (eg it's a library) - pub fn target_filenames( - &mut self, - unit: &Unit<'a>, - ) -> CargoResult, FileFlavor)>>> { - self.files.as_ref().unwrap().target_filenames(unit, self) + pub fn outputs(&mut self, unit: &Unit<'a>) -> CargoResult>> { + self.files.as_ref().unwrap().outputs(unit, self) } /// For a package, return all targets which are registered as dependencies diff --git a/src/cargo/ops/cargo_rustc/fingerprint.rs b/src/cargo/ops/cargo_rustc/fingerprint.rs index 0c55df23f..ab4731612 100644 --- a/src/cargo/ops/cargo_rustc/fingerprint.rs +++ b/src/cargo/ops/cargo_rustc/fingerprint.rs @@ -91,12 +91,12 @@ pub fn prepare_target<'a, 'cfg>( .join("index.html") .exists(); } else { - for &(ref src, ref link_dst, file_type) in cx.target_filenames(unit)?.iter() { - if file_type == FileFlavor::DebugInfo { + for output in cx.outputs(unit)?.iter() { + if output.flavor == FileFlavor::DebugInfo { continue; } - missing_outputs |= !src.exists(); - if let Some(ref link_dst) = *link_dst { + missing_outputs |= !output.path.exists(); + if let Some(ref link_dst) = output.hardlink { missing_outputs |= !link_dst.exists(); } } diff --git a/src/cargo/ops/cargo_rustc/mod.rs b/src/cargo/ops/cargo_rustc/mod.rs index 76b9d9afd..0e0807879 100644 --- a/src/cargo/ops/cargo_rustc/mod.rs +++ b/src/cargo/ops/cargo_rustc/mod.rs @@ -193,14 +193,14 @@ pub fn compile_targets<'a, 'cfg: 'a>( queue.execute(&mut cx)?; for unit in units.iter() { - for &(ref dst, ref link_dst, file_type) in cx.target_filenames(unit)?.iter() { - if file_type == FileFlavor::DebugInfo { + for output in cx.outputs(unit)?.iter() { + if output.flavor == FileFlavor::DebugInfo { continue; } - let bindst = match *link_dst { + let bindst = match output.hardlink { Some(ref link_dst) => link_dst, - None => dst, + None => &output.path, }; if unit.profile.test { @@ -208,7 +208,7 @@ pub fn compile_targets<'a, 'cfg: 'a>( unit.pkg.clone(), unit.target.kind().clone(), unit.target.name().to_string(), - dst.clone(), + output.path.clone(), )); } else if unit.target.is_bin() || unit.target.is_example() { cx.compilation.binaries.push(bindst.clone()); @@ -218,7 +218,7 @@ pub fn compile_targets<'a, 'cfg: 'a>( .libraries .entry(pkgid) .or_insert_with(HashSet::new) - .insert((unit.target.clone(), dst.clone())); + .insert((unit.target.clone(), output.path.clone())); } } @@ -243,14 +243,15 @@ pub fn compile_targets<'a, 'cfg: 'a>( continue; } - let v = cx.target_filenames(dep)?; + let outputs = cx.outputs(dep)?; cx.compilation .libraries .entry(unit.pkg.package_id().clone()) .or_insert_with(HashSet::new) .extend( - v.iter() - .map(|&(ref f, _, _)| (dep.target.clone(), f.clone())), + outputs + .iter() + .map(|output| (dep.target.clone(), output.path.clone())), ); } @@ -367,7 +368,7 @@ fn rustc<'a, 'cfg>( rustc.arg("--cap-lints").arg("warn"); } - let filenames = cx.target_filenames(unit)?; + let outputs = cx.outputs(unit)?; let root = cx.files().out_dir(unit); let kind = unit.kind; @@ -427,12 +428,12 @@ fn rustc<'a, 'cfg>( add_custom_env(&mut rustc, &build_state, ¤t_id, kind)?; } - for &(ref filename, ref _link_dst, _linkable) in filenames.iter() { + for output in outputs.iter() { // If there is both an rmeta and rlib, rustc will prefer to use the // rlib, even if it is older. Therefore, we must delete the rlib to // force using the new rmeta. - if filename.extension() == Some(OsStr::new("rmeta")) { - let dst = root.join(filename).with_extension("rlib"); + if output.path.extension() == Some(OsStr::new("rmeta")) { + let dst = root.join(&output.path).with_extension("rlib"); if dst.exists() { paths::remove_file(&dst)?; } @@ -482,7 +483,7 @@ fn rustc<'a, 'cfg>( } if do_rename && real_name != crate_name { - let dst = &filenames[0].0; + let dst = &outputs[0].path; let src = dst.with_file_name( dst.file_name() .unwrap() @@ -567,7 +568,7 @@ fn link_targets<'a, 'cfg>( unit: &Unit<'a>, fresh: bool, ) -> CargoResult { - let filenames = cx.target_filenames(unit)?; + let outputs = cx.outputs(unit)?; let package_id = unit.pkg.package_id().clone(); let target = unit.target.clone(); let profile = unit.profile.clone(); @@ -584,13 +585,14 @@ fn link_targets<'a, 'cfg>( // above. This means that `cargo build` will produce binaries in // `target/debug` which one probably expects. let mut destinations = vec![]; - for &(ref src, ref link_dst, _file_type) in filenames.iter() { + for output in outputs.iter() { + let src = &output.path; // This may have been a `cargo rustc` command which changes the // output, so the source may not actually exist. if !src.exists() { continue; } - let dst = match link_dst.as_ref() { + let dst = match output.hardlink.as_ref() { Some(dst) => dst, None => { destinations.push(src.display().to_string()); @@ -1071,8 +1073,8 @@ fn build_deps_args<'a, 'cfg>( current: &Unit<'a>, dep: &Unit<'a>, ) -> CargoResult<()> { - for &(ref dst, _, file_type) in cx.target_filenames(dep)?.iter() { - if file_type != FileFlavor::Linkable { + for output in cx.outputs(dep)?.iter() { + if output.flavor != FileFlavor::Linkable { continue; } let mut v = OsString::new(); @@ -1097,7 +1099,7 @@ fn build_deps_args<'a, 'cfg>( v.push("="); v.push(cx.files().out_dir(dep)); v.push(&path::MAIN_SEPARATOR.to_string()); - v.push(&dst.file_name().unwrap()); + v.push(&output.path.file_name().unwrap()); cmd.arg("--extern").arg(&v); } Ok(()) diff --git a/src/cargo/ops/cargo_rustc/output_depinfo.rs b/src/cargo/ops/cargo_rustc/output_depinfo.rs index a8a83425e..8edcd185f 100644 --- a/src/cargo/ops/cargo_rustc/output_depinfo.rs +++ b/src/cargo/ops/cargo_rustc/output_depinfo.rs @@ -91,8 +91,8 @@ pub fn output_depinfo<'a, 'b>(context: &mut Context<'a, 'b>, unit: &Unit<'a>) -> .map(|f| render_filename(f, basedir)) .collect::>>()?; - for &(_, ref link_dst, _) in context.target_filenames(unit)?.iter() { - if let Some(ref link_dst) = *link_dst { + for output in context.outputs(unit)?.iter() { + if let Some(ref link_dst) = output.hardlink { let output_path = link_dst.with_extension("d"); if success { let target_fn = render_filename(link_dst, basedir)?; -- 2.30.2